<?php
namespace app\mingteng\model;
use think\Model;
class Navigation extends Model {
    //对子栏目和父级栏目进行排序
    public function sort_list(){
        $navigationList = $this->where(['level'=>1,'pid'=>0])->order("sort")->select();
        foreach ($navigationList as $k=>$v){
            $navigationList[$k]['son'] = $this->where(['level'=>2,'pid'=>$v['id']])->order("sort")->select();
        }
        return $navigationList;
    }

    public function all_list(){
        global $category, $category2;
        $category = $this->order("pid,sort")->select();
        $category = convert_arr_key($category, 'id');
        foreach ($category AS $key => $value){
            if($value['level'] == 1)
                $this->get_cat_tree($value['id']);
        }
        return $category2;
    }

    public function get_cat_tree($id){
        global $category, $category2;
        $category2[$id] = $category[$id];
        $category2[$id]['have_son'] = 0;
        foreach ($category AS $key => $value){
            if($value['pid'] == $id){
                $this->get_cat_tree($value['id']);
                $category2[$id]['have_son'] = 1; // 还有下级
            }
        }
        return $category2;
    }
}
